package day_17.ChengYuanFangFa;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        //        Object invoke(Object obj, Object... args)：运行方法
//        参数一：用obj对象调用该方法
//        参数二：调用方法的传递的参数（如果没有就不写）
//        返回值：方法的返回值（如果没有就不写）

        //1.获取class对象
        Class<?> aClass = Class.forName("day_17.ChengYuanFangFa.Student");
        //2.获取里面的Method对象  function4
        Method method = aClass.getMethod("function4", String.class);
        //3.运行function4方法就可以了
        //3.1创建一个Student对象,当做方法的调用者
        Student student =(Student) aClass.newInstance();
        //3.2运行方法
        Object o = method.invoke(student, "杜某");
        //4.打印一下返回值
        System.out.println(o);
    }
}
class lx02{
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class aClass = Class.forName("day_17.ChengYuanFangFa.Student");
        Method method = aClass.getMethod("function4", String.class);
        Student student =(Student) aClass.newInstance();
        Object o = method.invoke(student,"李某");
        System.out.println(o);
    }
}
